package com.sias.dao.impl;

import com.sias.bean.Emp;
import com.sias.dao.EmpDao;
import com.sias.service.EmpService;
import com.sias.service.impl.EmpServiceImpl;
import com.sias.util.DbUtils;
import com.sun.xml.internal.bind.v2.model.core.ID;
import lombok.val;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * 员工数据库操作实现类
 **/
public class EmpDaoImpl implements EmpDao {

    /**
     * 实现查询员工功能
     */
    @Override
    public List<Emp> getAllEmp() {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> empList = null;
        try {
            empList = queryRunner.query("select * from emp", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return empList;
    }

    /**
     * 实现添加功能
     */
    @Override
    public void addEmp(Emp emp) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        Object[] parms;
        try {
            parms = new Object[]{ emp.getEname(), emp.getJob(), emp.getSal(), emp.getHiredate(), emp.getComm(), emp.getDeptno()};
            queryRunner.insert("insert into emp(ename,job,sal,hiredate,comm,deptno) value (?,?,?,?,?,?)",new BeanListHandler<Emp>(Emp.class),parms);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            System.out.println("插入成功！！！");
        }
    }



    /**
     * 根据编号删除员工
     */

    public void delEmpById(int emp){
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());

        try {
            queryRunner.update("DELETE FROM emp WHERE empmo_id = ?;",new BeanListHandler<Emp>(Emp.class),emp);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            System.out.println("");
        }
    }
    /**
     * 名字模糊查询
     */
    @Override
    public List<Emp> selectLikeName(String ename) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> empList = null;
        try {
            empList = queryRunner.query("select * from emp where ename like '%"+ename+"%';", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return empList;
    }

    /**
     * 修改员工信息
     */
    @Override
    public void upData(int empno) {
        Object[] params={empno};
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> empList = null;
        try {
            empList = queryRunner.query("select * from emp where empno=?", new BeanListHandler<Emp>(Emp.class),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (Emp emp : empList) {
            System.out.println(emp);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的名称：");
        String ename=sc.next();
        System.out.println("请输入要修改的工作：");
        String job=sc.next();
        System.out.println("请输入要修改的薪水：");
        double sal=sc.nextDouble();
        System.out.println("请输入要修改的奖金：");
        double comm=sc.nextDouble();
        System.out.println("请输入要修改的部门编号：");
        int deptno=sc.nextInt();
        Object[] params1={ename,job,sal,comm,deptno,empno};
        try {
            queryRunner.update("update emp set ename=?,job=?,sal=?,comm=?,deptno=? where empno=?",params1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

}
